package controllers.user;

import java.util.Collection;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.ProjectService;
import services.UserGroupService;
import services.VacancyService;
import controllers.AbstractController;
import domain.UserGroup;
import domain.Vacancy;
import forms.VacancyForm;

@Controller
@RequestMapping("/vacancy/user")
public class VacancyUserController extends AbstractController {

	@Autowired
	private VacancyService vacancyService;

	@Autowired
	private UserGroupService userGroupService;

	@Autowired
	private ProjectService projectService;

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView create(@RequestParam int projectId) {
		ModelAndView result;
		VacancyForm vacancyForm;
		Collection<UserGroup> userGroups;

		userGroups = userGroupService.findAvailableGroups(projectId);
		vacancyForm = new VacancyForm();
		vacancyForm.setProjectId(projectId);

		result = new ModelAndView("vacancy/create");
		result.addObject("vacancyForm", vacancyForm);
		result.addObject("requestURI", "vacancy/user/create.do");
		result.addObject("userGroups", userGroups);
		result.addObject("backURI", "/vacancy/user/list.do?projectId="
				+ projectId);

		return result;

	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public ModelAndView list(@RequestParam int projectId) {
		ModelAndView result;
		Collection<Vacancy> vacancies;
		String status;

		status = projectService.findOne(projectId).getStatus();
		vacancies = vacancyService.findByProject(projectId);

		result = new ModelAndView("vacancy/list");
		result.addObject("vacancies", vacancies);
		result.addObject("requestURI", "vacancy/user/list.do");
		result.addObject("projectId", projectId);
		result.addObject("statusProject", status);
		result.addObject("backURI", "/project/user/edit.do?projectId="
				+ projectId);

		return result;

	}

	// Edition
	@RequestMapping(value = "/create", method = RequestMethod.POST, params = "save")
	public ModelAndView save(@Valid VacancyForm vacancyForm,
			BindingResult binding) {
		ModelAndView result;
		Vacancy vacancy;

		if (binding.hasErrors()) {
			result = createEditModelAndView(vacancyForm);
		} else {
			try {

				vacancy = vacancyService.reconstruct(vacancyForm);

				vacancyService.save(vacancy);
				result = new ModelAndView(
						"redirect:/vacancy/user/list.do?projectId="
								+ vacancyForm.getProjectId());

			} catch (Throwable oops) {

				result = createEditModelAndView(vacancyForm,
						"vacancy.commit.error");
			}
		}
		return result;
	}

	// Ancillary methods
	protected ModelAndView createEditModelAndView(VacancyForm vacancyForm) {
		ModelAndView result;

		result = createEditModelAndView(vacancyForm, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(VacancyForm vacancyForm,
			String message) {
		ModelAndView result;

		result = new ModelAndView("vacancy/create");

		result.addObject("vacancyForm", vacancyForm);
		result.addObject("message", message);

		return result;
	}

}
